document.addEventListener('DOMContentLoaded', function() {
    const registerForm = document.getElementById('registerForm');

    registerForm.addEventListener('submit', async function(e) {
        e.preventDefault();

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const nickname = document.getElementById('nickname').value;

        try {
            const response = await fetch('/api/v1/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    nickname: nickname
                })
            });

            const data = await response.json();

            if (response.ok) {
                alert('注册成功，请登录');
                window.location.href = '/login';
            } else {
                alert(data.msg || '注册失败');
            }
        } catch (error) {
            console.error('注册失败:', error);
            alert('注册失败，请稍后重试');
        }
    });
}); 